# coding: utf-8
require "open-uri"
require 'net/http'
class AdminController < ApplicationController

  # GET: 反馈列表
  def feedback
    return unless checkAdmin
    per_page = params[:per] || 10
  	@feedback = Feedback.new
  	@feedlist = Feedback.paginate(:page => params[:page], :per_page => per_page).order('id DESC')
  end

  # POST: 保存回复反馈的内容
  def feedback_reply
  	_success = false
  	feed = Feedback.find(params[:id])
  	if(feed != nil)
  		feed.reply = params[:reply]
  		_success = feed.save
      replymail(feed.name, feed.email, feed.reply) if(_success)
  	end
    render :text => if(_success) then '1' else '0' end
  end

  # 将回复内容发送邮件
  def replymail(name, mail, reply)
    if(mail.include? '@' and mail.include? '.')
      name = '朋友' if(name == '匿名' || name == '')
      UserMailer.reply(name, mail, reply).deliver
      p '已成功发送 ---- '
    end
  end

  # POST: 删除反馈信息
  def feedback_delete
  	_success = false
  	feed = Feedback.find(params[:id])
  	if(feed != nil)
  		_success = feed.destroy
  	end
    render :text => if(_success) then '1' else '0' end
  end

  # POST: 批量删除
  def feedback_deletes
    count = 0;
    feedbacks = Feedback.find(params[:ids].split(','))
    feedbacks.each do |feed|
      feed.destroy
      count = count.next;
    end
    render :text => count
  end

  # GET: 短信填写页面
  def sms
  	@sms = User.new
  	@sms_count = getSmsCount

  	# render :text => getSmsResult('-2')
  end

  # POST: 发送短信
  def sms_send
  	content = params[:content]
  	nums = params[:nums]
  	pass = params[:pass]

  	if(content == nil || content.length < 2)
  		render :text => "内容不能为空。<a href='/admin/sms'>返回</a>"
  		return
  	end

  	nums = formatNums(nums) if(nums != nil) # 格式化手机号码
  	if(nums == nil || nums.length < 1)
  		if(nums.length < 11)
  			render :text => "手机号码不能为空。<a href='/admin/sms'>返回</a>"
  			return
  		end
  	end

  	if(pass == nil || pass != 'xinye.sms')
  		render :text => "密码错误。<a href='/admin/sms'>返回</a>"
  		return
  	end

  	# 发送短信
  	result = send_sms(content, nums)
  	if(result != nil and result.length < 3)
  		result = '已成功发送 '+ result +' 条'
  	end

  	flash['result'] = result
  	redirect_to action: "sms"
  end

private
	
	def getSmsCount
		url = 'http://www.smsgate.cn/ubal.asp?usr=13426269495'
		content = open(url).read();
	end

	def send_sms(content, nums)
		#return '1'
		url = URI.parse('http://www.smsgate.cn/utf.asp');
		Net::HTTP.start(url.host, url.port) do |http|
			req = Net::HTTP::Post.new(url.path)
			req.set_form_data({ 'usr' => '13426269495', 'pwd' => 'silverlight', 'tel' => nums, 'msg' => content, 'lng' => 1 })
			return http.request(req).body
		end
		'0'
	end

	# 格式化手机号码列表
	def formatNums(str)
		result = ''
		arr = str.split(/\r|\n/)
		arr.each do |line|
			line = line.lstrip.rstrip
			if(line.length == 11)
				result << line << ';'
			end
		end
		if(result != '')
			result = result[0, result.length - 1]
		end
		result
	end

	# 格式化短信发送结果
	def getSmsResult(code)
		case code
			when '-1' then '该用户不存在，您尚未在本站注册'
			when '-2' then '本站系统错误'
			when '-3' then '您的短信余额不足'
			when '-4' then '无发送内容'
			when '-5' then '无接收号码或接收号码的格式不正确'
			when '-6' then '网络通信错误或接收号码在事实上并不存在'
			when '-7' then '密码错误或来访IP地址非法'
			when '-8' then '用户帐户尚未激活'
			when '0' then '未知错误'
			else code
		end
	end

end
